local yeXuanhuo = fk.CreateSkill({
  name = "ye__xuanhuo",
  tags = { Skill.Compulsory },
})

Fk:loadTranslationTable{
  ["ye__xuanhuo"] = "眩惑",
  [":ye__xuanhuo"] = "锁定技，游戏开始时、回合结束时，你令所有其他角色获得“眩”标记。" ..
  "有“眩”标记的角色获得牌时，你随机获得其一张手牌。",
  ["@@ye__xuanhuo"] = "眩",
  ["#ye__xuanhuo-invoke"] = "眩惑：你可以随机获得%dest的一张手牌",
}

yeXuanhuo:addLoseEffect(function (self, player, is_death)
  local room = player.room
  if table.every(room.alive_players, function (p)
    return not p:hasSkill(yeXuanhuo.name, true)
  end) then
    for _, p in ipairs(room.alive_players) do
      room:setPlayerMark(p, "@@ye__xuanhuo", 0)
    end
  end
end)

yeXuanhuo:addEffect(fk.GameStart, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(yeXuanhuo.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, yeXuanhuo.name)
    player:broadcastSkillInvoke(yeXuanhuo.name, 1)
    for _, p in ipairs(room:getOtherPlayers(player, false)) do
      room:setPlayerMark(p, "@@ye__xuanhuo", 1)
    end
  end,
})
yeXuanhuo:addEffect(fk.TurnEnd, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(yeXuanhuo.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, yeXuanhuo.name)
    player:broadcastSkillInvoke(yeXuanhuo.name, 1)
    for _, p in ipairs(room:getOtherPlayers(player, false)) do
      room:setPlayerMark(p, "@@ye__xuanhuo", 1)
    end
  end,
})

yeXuanhuo:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yeXuanhuo.name) then
      local mark = player:getTableMark("ye__xuanhuo_count")
      local xuanhuoTargets = event:getSkillData(self, "ye__xuanhuo_" .. player.id)
      if xuanhuoTargets then
        return #xuanhuoTargets.unDone
      end

      for _, move in ipairs(data) do
        local to = move.to
        if
          to and
          move.toArea == Card.PlayerHand and
          to ~= player and
          not to:isKongcheng() and
          to:getMark("@@ye__xuanhuo") > 0 and
          (mark[tostring(to.id)] or 0) < 99
        then
          return true
        end
      end
    end
  end,
  trigger_times = function(self, event, target, player, data)
    if not player:hasSkill(yeXuanhuo.name) then
      return 0
    end

    local mark = player:getTableMark("ye__xuanhuo_count")

    local xuanhuoTargets = event:getSkillData(self, "ye__xuanhuo_" .. player.id)
    if xuanhuoTargets then
      local unDoneTargets = table.simpleClone(xuanhuoTargets.unDone)
      for _, to in ipairs(unDoneTargets) do
        if not to:isAlive() or to:isKongcheng() or (mark[tostring(to.id)] or 0) > 4 then
          table.remove(xuanhuoTargets.unDone, 1)
        else
          break
        end
      end

      return #xuanhuoTargets.unDone + #xuanhuoTargets.done
    end

    local xuanhuoTos = { done = {}, unDone = {} }
    for _, move in ipairs(data) do
      local to = move.to
      if
        to and
        move.toArea == Card.PlayerHand and
        to ~= player and
        not to:isKongcheng() and
        to:getMark("@@ye__xuanhuo") > 0 and
        (mark[tostring(to.id)] or 0) < 99
      then
        table.insert(xuanhuoTos.unDone, to)
      end
    end

    if #xuanhuoTos.unDone > 0 then
      player.room:sortByAction(xuanhuoTos.unDone)
      event:setSkillData(self, "ye__xuanhuo_" .. player.id, xuanhuoTos)
    end
    return #xuanhuoTos.unDone
  end,
  on_cost = function(self, event, target, player, data)
    local xuanhuoTargets = event:getSkillData(self, "ye__xuanhuo_" .. player.id)
    local to = table.remove(xuanhuoTargets.unDone, 1)
    table.insert(xuanhuoTargets.done, player)
    event:setSkillData(self, "ye__xuanhuo_" .. player.id, xuanhuoTargets)

    event:setCostData(self, to)
    return true
  end,
  on_trigger = function(self, event, target, player, data)
    event:setSkillData(self, "cancel_cost", false)
    self:doCost(event, target, player, data)
    event:setSkillData(self, "cancel_cost", false)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

    if player.dead then return end
    local to = event:getCostData(self)
    local mark = player:getTableMark("ye__xuanhuo_count")
    local count = mark[tostring(to.id)] or 0
    if not to:isKongcheng() and count < 99 then
      mark[tostring(to.id)] = count + 1
      room:setPlayerMark(player, "ye__xuanhuo_count", mark)
      room:obtainCard(player, table.random(to:getCardIds("h")), false, fk.ReasonPrey)
    end
  end,
})

return yeXuanhuo
